import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
    history: createWebHistory('/'),
    routes: [
        {
            path: '/',
            component: () => import('../views/layout.vue'),
            children: [
                {
                    path: '/',
                    component: () => import('../views/home/index.vue'),
                },
                {
                    name: 'category',
                    path: '/category',
                    component: () => import('../views/category/index.vue'),
                },
                {
                    name: 'sub',
                    path: '/sub',
                    component: () => import('../views/sub/index.vue'),
                },
                {
                    name: 'goods',
                    path: '/goods',
                    component: () => import('../views/goods/index.vue'),
                },
                {
                    name: 'order',
                    path: '/order',
                    component: () => import('../views/order/index.vue'),
                },
                {
                    name: 'business',
                    path: '/business',
                    component: () => import('../views/business/index.vue'),
                },
                {
                    name: 'user',
                    path: '/user',
                    component: () => import('../views/user/index.vue'),
                },
                {
                    name: 'rotation',
                    path: '/rotation',
                    component: () => import('../views/rotation/index.vue'),
                },
            ],
            beforeEnter(to, from, next) {
                try {
                    if (JSON.parse(localStorage.adminManage).profile.token) {
                        next()
                    } else {
                        router.push('/login')
                    }
                } catch (error) {
                    router.push('/login')
                }
            },
        },
        {
            path: '/login',
            component: () => import('../views/login/index.vue'),
        },
    ],
    scrollBehavior() {
        // 延时滚动
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve({ left: 0, top: 0, behavior: 'smooth' })
            }, 100)
        })
    },
})

export default router
